package com.buss.article.utils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ToolsUtil {
	/**
	 * 类型装换
	 * 适配模式
	 */
	public static Map<String, Object> mapOver(Map<String, String> parames) {
		Map<String, Object> result = new HashMap<>();
		Set<String> paramesKeys = parames.keySet();
		
		for(String temp : paramesKeys) {
			if(parames.get(temp) != null) {
				result.put(temp, parames.get(temp));
			}
		}
		
		return result;
	}
	
	public static Map convertBean(Object bean)   
            throws IntrospectionException, IllegalAccessException, InvocationTargetException {   
        Class type = bean.getClass();   
        Map returnMap = new HashMap();   
        BeanInfo beanInfo = Introspector.getBeanInfo(type);   
   
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();   
        for (int i = 0; i< propertyDescriptors.length; i++) {   
            PropertyDescriptor descriptor = propertyDescriptors[i];   
            String propertyName = descriptor.getName();   
            if (!propertyName.equals("class")) {   
                Method readMethod = descriptor.getReadMethod();   
                Object result = readMethod.invoke(bean, new Object[0]);   
                if (result != null) {   
                    returnMap.put(propertyName, result);   
                } else {   
                    returnMap.put(propertyName, "");   
                }   
            }   
        }   
        return returnMap;   
    }   
	
		
	
}
